* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/data/merged_for_reg, clear	
	
	xtset ctycode year

* set reference bin

	drop *dd5bin10
	
	* LD
********************************************************************************
*************long difference model(Figure 7)************************************
********************************************************************************
		foreach x in aoutput farming_output forest_output livestock_output fishery_output{
			rangestat (mean) ld_`x' = `x', interval(year -4,0) by(ctycode)
			}

		foreach x in dd5binNeg dd5bin0 dd5bin5 dd5bin15 dd5bin20 dd5bin25 dd5bin30 dd5bin35 pre ssd rhu prs win {
			rangestat (mean) ld_`x' = `x', interval(year -4,0) by(ctycode)
			}			
			
		foreach x in pre ssd rhu prs win {
			gen `x'2 = `x'^2			
			rangestat (mean) ld_`x'2 = `x'2, interval(year -4,0) by(ctycode)
			}
			
		foreach x in aoutput farming_output forest_output livestock_output fishery_output{
			gen ln_ld_`x' = log(ld_`x')
			}
	
		tempfile r1 r2
	
		foreach x in farming_output forest_output livestock_output fishery_output{
				qui reghdfe ln_ld_`x' ld_dd5bin* ///
					c.ld_pre##c.ld_pre c.ld_ssd##c.ld_ssd c.ld_rhu##c.ld_rhu c.ld_prs##c.ld_prs c.ld_win##c.ld_win ///
					i.pro#c.year##c.year ///
					if inlist(year,2000,2005,2010,2015) & region3==1, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store n_`x'_ld5
		parmest , label saving(`"`r1'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")
				
				qui reghdfe ln_ld_`x' ld_dd5bin* ///
					c.ld_pre##c.ld_pre c.ld_ssd##c.ld_ssd c.ld_rhu##c.ld_rhu c.ld_prs##c.ld_prs c.ld_win##c.ld_win ///
					i.pro#c.year##c.year ///
					if inlist(year,2000,2005,2010,2015) & region3==2, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
				estimates store s_`x'_ld5			
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")		
				
							preserve 
			use `"`r1'"',clear
			append using `"`r2'"'
			gen sector = "`x'"
			rename id  spec
			save $temp/est_ld5_`x',replace
			restore			
			
			}
				

		*esttab s_*_ma*, b(4) se(4) drop(*year* _cons) brackets	r2 star(* 0.1 ** 0.05 *** 0.01)		

		
	********************************************************
	*************** arrange the coefficient ****************
	********************************************************
	clear
	foreach file in farming_output forest_output livestock_output fishery_output {
		append using $temp/est_ld5_`file'
	}
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = -2.5 if strmatch(parm, "*dd5binNeg")
	replace level = 2.5 if strmatch(parm, "*dd5bin0")
	replace level = 7.5 if strmatch(parm, "*dd5bin5")
	*replace level = 12.5 if strmatch(parm, "*dd5bin10")
	replace level = 17.5 if strmatch(parm, "*dd5bin15")
	replace level = 22.5 if strmatch(parm, "*dd5bin20")
	replace level = 27.5 if strmatch(parm, "*dd5bin25")
	replace level = 32.5 if strmatch(parm, "*dd5bin30")
	replace level = 37.5 if strmatch(parm, "*dd5bin35")


	// sector name 
	replace sector = "Cropping" if strmatch(sector,"farming_output")
	replace sector = "Livestock" if strmatch(sector,"livestock_output")
	replace sector = "Forestry" if strmatch(sector,"forest_output")
	replace sector = "Fishery" if strmatch(sector,"fishery_output")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort spec sector : gen last = _n == _N
	expand 2 if last
	sort spec sector parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]		
	
	save $visual/visual_ld5,replace
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}
			

			
			
			
			
	
